package com.generator.controller;

import com.generator.entity.Template;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.generator.service.TemplateService;
import com.generator.utils.*;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 代码生成器模板 前端控制器
* </p>
* @author miao
* @since 2024-01-19
*/
@RestController
@RequestMapping("/template")
    public class TemplateController {
    @Autowired
    private TemplateService service;


    @Operation(summary = "分页列表")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "currentPage", value = "当前页码", paramType = "query", dataType = "int"),
    @ApiImplicitParam(name = "pageSize", value = "每页显示个数", paramType = "query", dataType = "int")
    })
    @GetMapping(value = "/page")
    public Result page(@RequestParam Map<String, Object> params) {
        PageData<Template> page = service.Page(params);
        //返回结果集
        return new Result().ok(page);
    }

    /**
    * 添加信息
    */
    @PostMapping("add")
    @Operation(summary = "添加信息")
    public Result add(@RequestBody Template entity) {
        int n = service.add(entity);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "添加失败");
        }
    }

    /**
    * 编辑信息
    */
    @PutMapping("update")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody Template entity) {
        int n = service.update(entity);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "编辑失败");
        }
    }


    /**
    * 根据ids删除信息
    */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ids删除信息")
    public Result del(@PathVariable("id") Long id){
        int n = service.delete(id);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "删除失败");
        }
    }
}
